package com.fhminyi.GUI;

import com.fhminyi.dao.ProductDao;
import com.fhminyi.model.Product;
import com.fhminyi.model.User;
import com.fhminyi.util.ImageUploadSelector;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

public class AddProductFrame extends JFrame {
    private User currentUser;
    private JTextField nameField;
    private JTextArea descriptionArea;
    private JTextField priceField;
    private JTextField stockField;
    private JTextField categoryField;
    private JButton submitButton;
    private JButton uploadButton;
    private ImageUploadSelector imageUploadSelector;
    private Product productToEdit;

    public AddProductFrame(User currentUser) {
        this(currentUser, null);
    }

    public AddProductFrame(User currentUser, Product product) {
        this.currentUser = currentUser;
        this.productToEdit = product;
        setTitle(product == null ? "添加闲置物品" : "编辑商品");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(8, 2));

        JLabel nameLabel = new JLabel("商品名称:");
        nameField = new JTextField();

        JLabel descriptionLabel = new JLabel("描述:");
        descriptionArea = new JTextArea(3, 20);
        descriptionArea.setLineWrap(true);
        JScrollPane descriptionScrollPane = new JScrollPane(descriptionArea);

        JLabel priceLabel = new JLabel("价格:");
        priceField = new JTextField();

        JLabel stockLabel = new JLabel("库存:");
        stockField = new JTextField();

        JLabel categoryLabel = new JLabel("分类:");
        categoryField = new JTextField();

        uploadButton = new JButton("上传图片");
        uploadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                imageUploadSelector.showFileChooser(AddProductFrame.this);
            }
        });

        submitButton = new JButton("提交");
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = nameField.getText().trim();
                String description = descriptionArea.getText().trim();
                String priceStr = priceField.getText().trim();
                String stockStr = stockField.getText().trim();
                String category = categoryField.getText().trim();

                if (name.isEmpty() || description.isEmpty() || priceStr.isEmpty() || stockStr.isEmpty() || category.isEmpty() || imageUploadSelector.getSelectedFile() == null) {
                    JOptionPane.showMessageDialog(AddProductFrame.this, "所有字段都必须填写，并选择图片", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                try {
                    double price = Double.parseDouble(priceStr);
                    int stock = Integer.parseInt(stockStr);

                    Product product = productToEdit != null ? productToEdit : new Product();
                    product.setUserId(currentUser.getUserId());
                    product.setProductName(name);
                    product.setDescription(description);
                    product.setPrice(price);
                    product.setStock(stock);
                    product.setCategory(category);

                    imageUploadSelector.uploadProductImage(product);

                    ProductDao productDao = new ProductDao();
                    if (productToEdit != null) {
                        productDao.updateProduct(product);
                    } else {
                        productDao.addProduct(product);
                    }

                    JOptionPane.showMessageDialog(AddProductFrame.this, productToEdit != null ? "商品编辑成功" : "商品添加成功");
                    dispose();
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(AddProductFrame.this, "价格和库存必须是数字", "错误", JOptionPane.ERROR_MESSAGE);
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(AddProductFrame.this, "操作商品失败", "错误", JOptionPane.ERROR_MESSAGE);
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(AddProductFrame.this, "上传文件失败", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        panel.add(nameLabel);
        panel.add(nameField);
        panel.add(descriptionLabel);
        panel.add(descriptionScrollPane);
        panel.add(priceLabel);
        panel.add(priceField);
        panel.add(stockLabel);
        panel.add(stockField);
        panel.add(categoryLabel);
        panel.add(categoryField);
        panel.add(uploadButton);
        panel.add(submitButton);

        add(panel);

        imageUploadSelector = new ImageUploadSelector();

        if (productToEdit != null) {
            nameField.setText(productToEdit.getProductName());
            descriptionArea.setText(productToEdit.getDescription());
            priceField.setText(String.valueOf(productToEdit.getPrice()));
            stockField.setText(String.valueOf(productToEdit.getStock()));
            categoryField.setText(productToEdit.getCategory());
        }
    }
}